import {createAction} from 'redux-actions';
import {
  GET_USER_INFO,
  PUT_USER_INFO,
  GET_USER_SIGN,
  GET_USER_CHATS,
  GET_USER_SHARE,
  GET_USER_COLLECT,
  DEL_USER_COLLECT,
  GET_USER_HISTORY,
  GET_USER_ORDER,
  POST_USER_COLLECT,
  POST_USER_HISTORY,
  GET_USER_LIKE,
  POST_USER_RELATION,
  POST_USER_ADID,
} from './../constants/actionTypes'
import * as userApi from './../apis/user'

export const userInfo = createAction(GET_USER_INFO, async(params)=>{
  return await userApi.getUserInfo(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'userInfoError'
    })
}, (params)=>{
  return {
    ...params,
    sync: 'userInfo'
  }
})


export const updateUser = createAction(PUT_USER_INFO, async(params)=>{
  return await userApi.putUserInfo(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'putUserError'
    })
}, (params)=>{
  return {
    ...params,
    sync: 'putUser'
  }
})


export const saveRelation = createAction(POST_USER_RELATION, async(params)=>{
  return await userApi.postSaveRelation(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'saveRelationError'
    })
}, (params)=>{
  return {
    ...params,
    sync: 'saveRelation'
  }
})

export const userSign = createAction(GET_USER_SIGN, async()=>{
  return await userApi.getUserSign()
    .then((data) =>{
      if (data){
        return data
      }
      throw 'userSignError'
    })
}, ()=>{
  return {
    sync: 'userSign'
  }
})

export const userChats = createAction(GET_USER_CHATS, async()=>{
  return await userApi.getUserChats()
    .then((data) =>{
      if (data){
        return data
      }
      throw 'userChatsError'
    })
}, ()=>{
  return {
    sync: 'userChats'
  }
})


export const userShare = createAction(GET_USER_SHARE, async()=>{
  return await userApi.getUserShare()
    .then((data) =>{
      if (data){
        return data
      }
      throw 'userShareError'
    })
}, ()=>{
  return {
    sync: 'userShare'
  }
})

export const userCollect = createAction(GET_USER_COLLECT, async()=>{
  return await userApi.getUserCollect()
    .then((data) =>{
      if (data){
        return data
      }
      throw 'userCollectError'
    })
}, ()=>{
  return {
    sync: 'userCollect'
  }
})


export const delUserCollect = createAction(DEL_USER_COLLECT, async(id)=>{
  return await userApi.delUserCollect(id)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'delUserCollectError'
    })
}, (id)=>{
  return {
    id,
    sync: 'delUserCollect'
  }
})


export const getUserHistory = createAction(GET_USER_HISTORY, async()=>{
  return await userApi.getUserHistory()
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getUserHistoryError'
    })
}, ()=>{
  return {
    sync: 'getUserHistory'
  }
})

export const getUserOrder = createAction(GET_USER_ORDER, async()=>{
  return await userApi.getUserOrder()
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getUserOrderError'
    })
}, ()=>{
  return {
    sync: 'getUserOrder'
  }
})


export const postUserHistory = createAction(POST_USER_HISTORY, async(params)=>{
  return await userApi.postHistory(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'postUserHistoryError'
    })
}, (params)=>{
  return {
    ...params,
    sync: 'postUserHistory'
  }
})



export const getUserLike = createAction(GET_USER_LIKE, async(id)=>{
  return await userApi.getLike(id)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getUserLikeError'
    })
}, (id)=>{
  return {
    id,
    sync: 'getUserLike'
  }
})


export const postUserCollect = createAction(POST_USER_COLLECT, async(params)=>{
  return await userApi.postCollect(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'postUserCollectError'
    })
}, (params)=>{
  return {
    ...params,
    sync: 'postUserCollect'
  }
})


export const postAdid = createAction(POST_USER_ADID, async(params)=>{
  return await userApi.postAdid(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'postAdidError'
    })
}, (params)=>{
  return {
    ...params,
    sync: 'postAdidCollect'
  }
})